package cn.ibdsr.human.controller;

import cn.ibdsr.human.constant.BizConstants;
import cn.ibdsr.human.constant.Const;
import cn.ibdsr.human.constant.Result;
import cn.ibdsr.human.constant.ServerInitEnum;
import cn.ibdsr.human.db.mapper.CompanyMapper;
import cn.ibdsr.human.db.mapper.ServerInitMapper;
import cn.ibdsr.human.db.mapper.UserMapper;
import cn.ibdsr.human.db.model.ServerInit;
import cn.ibdsr.human.exception.BizExceptionEnum;
import cn.ibdsr.human.exception.BusinessException;
import cn.ibdsr.human.manager.LogFormatManager;
import cn.ibdsr.human.service.CompanyRecommendService;
import cn.ibdsr.human.service.CompanyService;
import cn.ibdsr.human.service.JobService;
import cn.ibdsr.human.transfer.ServerInitDTO;
import cn.ibdsr.human.transfer.company.CompanyListDTO;
import cn.ibdsr.human.transfer.company.CompanyResponseDTO;
import cn.ibdsr.human.transfer.company.CompanySaveDTO;
import cn.ibdsr.human.transfer.companyrecommend.CompanyRecommendDTO;
import cn.ibdsr.human.transfer.companyrecommend.CompanyRecommendListDTO;
import cn.ibdsr.human.transfer.companyrecommend.CompanyRecommendQueryDTO;
import cn.ibdsr.human.transfer.job.JobOperatorDTO;
import cn.ibdsr.human.transfer.platform.CompanyQueryDTO;
import cn.ibdsr.human.transfer.platform.LatestJobItem;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: cdl
 * @email: iamcimon@qq.com
 * @date: 2021/8/6 15:14
 */
@Api(tags = "推荐企业管理控制器")
@RestController
@RequestMapping(value = "/data-center")
@Slf4j
@Validated
public class DataCenterController {
    @Resource
    private ServerInitMapper serverInitMapper;
    @Resource
    private JobService jobService;
    @Resource
    private CompanyRecommendService companyRecommendService;
    @Resource
    private LogFormatManager logFormatManager;
    /**
     * cdl-ok
     * 获取进度
     */
    @ApiOperation("获取客服电话")
    @GetMapping("/phone")
    @ResponseBody
    public Result<ServerInitDTO> getPhone(  ) {
        QueryWrapper<ServerInit> serverInitQueryWrapper=new QueryWrapper<>();
        serverInitQueryWrapper.eq("type", ServerInitEnum.SERVER_PHONE.getType());
        ServerInit serverInit=serverInitMapper.selectOne(serverInitQueryWrapper);
        if(serverInit==null){
            throw new BusinessException(BizExceptionEnum.SERVIER_INIT_ERROR);
        }
        ServerInitDTO serverInitDTO=new ServerInitDTO();
        serverInitDTO.setTelePhone(String.valueOf(serverInit.getConfigValue()));
        return new Result<>(serverInitDTO);
    }
    /**
     * cdl-ok
     * 获取进度
     */
    @ApiOperation("获取最新岗位信息")
    @GetMapping("/jobs")
    @ResponseBody
    public Result<List<LatestJobItem>> getLatestJob() {
        List<LatestJobItem> latestJobItems=jobService.getLatestJob();
        return new Result<>(latestJobItems);
    }


    /**
     * cdl-ok
     * 获取个人信息
     */
    @ApiOperation("获取企业信息")
    @GetMapping("/companies/{id}")
    @ResponseBody//
    public Result<CompanyRecommendDTO> getCompanyInformation( @PathVariable("id") Long id) {
        // Long userId=1L;
        logFormatManager.formatKeyPointLog("获取企业信息","id="+id, BizConstants.MODULE_DATA_CENTER);
        CompanyRecommendDTO companyRecommendDTO=companyRecommendService.getCompanyInformation(id);
        return new Result<>(companyRecommendDTO);
    }

    /**
     * cdl-ok
     * 保存个人信息
     */
    @ApiOperation("创建和编辑企业信息：编辑企业信息要带id参数，创建不带")
    @PostMapping
    @ResponseBody
    public Result<Long> save(@RequestAttribute(name = Const.USER_ID  ) Long userId,  @RequestBody @Valid CompanyRecommendDTO companyRecommendDTO) {
        logFormatManager.formatKeyPointLog("保存企业信息","userId="+userId+":"+ JSONObject.toJSONString(companyRecommendDTO), BizConstants.MODULE_DATA_CENTER);
        Long flag=companyRecommendService.save(userId,companyRecommendDTO);
        return new Result<>(flag);
    }

    ////////////////////////针对企业的相关接口////////////////////
    /**
     * cdl-ok
     * 发布岗位
     */
    @ApiOperation("平台分页获取推荐企业列表")
    @GetMapping("/companies")//@RequestAttribute(name = Const.USER_ID  )
    @ResponseBody
    public Result<CompanyRecommendListDTO> getCompanies(@Valid CompanyRecommendQueryDTO companyRecommendQueryDTO) {
        logFormatManager.formatKeyPointLog("平台分页获取推荐企业列表", JSONObject.toJSONString(companyRecommendQueryDTO), BizConstants.MODULE_DATA_CENTER);
        CompanyRecommendListDTO flag = companyRecommendService.getCompanies(companyRecommendQueryDTO);
        return new Result<>(flag);
    }

    /**
     * cdl-ok
     * 发布岗位
     */
    @ApiOperation("发布企业/取消发布")
    @PutMapping("/publish-statuses/{id}")//@RequestAttribute(name = Const.USER_ID  )
    @ResponseBody
    public Result<Long> publishOrNot( @RequestAttribute(name = Const.USER_ID  )  Long userId,@PathVariable("id") Long id,   @RequestBody JobOperatorDTO jobSaveDTO) {
        jobSaveDTO.setId(id);
        logFormatManager.formatKeyPointLog("发布企业/取消发布","userId="+userId+":"+JSONObject.toJSONString(jobSaveDTO), BizConstants.MODULE_DATA_CENTER);
        Long flag=companyRecommendService.publishOrNot(userId,jobSaveDTO);
        return new Result<>(flag);
    }

    /**
     * cdl-ok
     * 发布岗位
     */
    @ApiOperation("删除企业")
    @DeleteMapping("/companies/{id}")//@RequestAttribute(name = Const.USER_ID  )
    @ResponseBody
    public Result<Long> delete( @RequestAttribute(name = Const.USER_ID  )  Long userId,@PathVariable("id") Long id) {
        logFormatManager.formatKeyPointLog("删除企业","userId="+userId+": id="+id, BizConstants.MODULE_DATA_CENTER);
        Long flag=companyRecommendService.delete(userId,id);
        return new Result<>(flag);
    }

}
